<template>
  <el-dialog
    title="新增员工"
    :visible="dialogFormVisible"
    @close="closeDialog"
    @click.native="showTree = false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width:50%"
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width:50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width:50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width:50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width:50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          readonly
          style="width:50%"
          placeholder="请选择部门"
          @click.native.stop="getDepartments"
        />
        <div v-if="showTree" class="tree-box">
          <el-tree
            v-loading="loading"
            :data="depts"
            :props="{ label: 'name' }"
            @node-click="selectNode"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width:50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="confirmSubmit">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartmentsApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/constant/employees'
import { addEmployeeApi } from '@/api/employees'
export default {
  props: {
    dialogFormVisible: {
      required: true,
      type: Boolean
    }
  },
  data() {
    return {
      formData: {
        // departmentName: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户姓名不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 2,
            max: 4,
            message: '用户姓名为2-4位',
            trigger: ['blur', 'change']
          }
        ],
        mobile: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: ['blur', 'change']
          },
          {
            // 可以自定义，也可以pattern
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: ['blur', 'change']
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '请选择入职时间',
            trigger: ['blur', 'change']
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: '聘用形式不能为空',
            trigger: ['blur', 'change']
          }
        ],
        workNumber: [
          {
            required: true,
            message: '工号不能为空',
            trigger: ['blur', 'change']
          }
        ],
        departmentName: [
          {
            required: true,
            message: '部门不能为空',
            trigger: ['change', 'blur']
          }
        ]
      },
      depts: [],
      showTree: false,
      loading: false,
      EmployeeEnum, // 必须挂载在vue实例上，因为template里有this,如果script使用可以不用挂载，直接用
      formLoading: false
    }
  },
  methods: {
    //   关闭弹窗
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:dialogFormVisible', false)
    },
    // 确认
    confirmSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const { data } = await addEmployeeApi(this.formData)
          this.$message.success(data.message)
          this.closeDialog()
          //   console.log(this)
          //   this.$parent.getEmployeeList()
          this.$emit('upData')
        }
      })
    },
    // 获取部门树状数据=》点击触发
    async getDepartments() {
      this.showTree = !this.showTree
      if (!this.showTree) return
      this.loading = true
      const {
        data: { data }
      } = await getDepartmentsApi()
      //   console.log(data)
      this.depts = tranListToTreeData(data.depts, '')
      this.loading = false
    },
    // 点击树状图的回调函数
    selectNode(nodeData) {
      if (nodeData.children && nodeData.children.length > 0) {
        //   return语句将终止当前函数并返回当前函数的值
        return
      }
      //   console.log(nodeData)
      //   原因是，我们直接采用了空对象初始化的形式，来初始化对象，这样对象中的字段在赋值的时候是不存在响应式的
      //   this.formData.departmentName = nodeData.name
      // 可以通过this.$set()的方式来将对象进行响应式的赋值
      // this.$set()方法中:
      // 第一个参数是你需要设置data中的属性的外层，这里我要设置的是data中的form，按照原来的赋值写法就是this.form='xx',那么form的外层就是this,
      // 第二个参数就是你要设置的具体对象，注意使用字符串的形式，这里我要对form属性进行设置，需要就填写form
      // 第三个参数就是相当于你要设置的实际值

      this.$set(this.formData, 'departmentName', nodeData.name)
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
}
</style>
